import storage from '@/utils/ado-self/ado-storage';
import componentAxios from 'axios';
const axios = window._xvueAxios || window._xvueAxiosProxy || componentAxios; // 移到编排系统兼容，_xvueAxios是xvue的
import { $message } from '@/utils/util.js';

export default {
  getRegionsByApi() {
    return new Promise(resolve => window
      ._UIDesingerAxios({
        url: '/dcp/data/data/v3/getByApiCode', // 获取单个区域数据
        method: 'post',
        data: {
          apiCode: 'getProAreaCountryList',
          params: [
            {
              code: 'grid_code',
              value: window._adoPremise.topRegion,
            },
          ],
        },
      })
      .then(res => {
        if (res.status === 403) {
          $message({
            type: 'warning',
            message: '当前用户角色缺少【xminer 根据编排API查询数据】权限，请到IAM进行授权',
          });
        }
        const _regions = res.data.data;
        if (_regions) {
          resolve(_regions);
        }
      })
      .catch(err => {
        resolve([]);
      }));
  },
  componentGetRegions() {
    const topRegion =
      window._adoPremise.topRegion ||
      storage.get('top_region') ||
      window._xvueStore.state.region.topRegion ||
      window._xvueStore.state.region.loginRegionCode;

    // 获取前三层区域数据
    const params = {
      apiCode: 'getProAreaCountryList',
      params: [{ code: 'grid_code', value: topRegion }],
    };
    return new Promise(resolve => {
      axios({
        method: 'post',
        url: '/dcp/data/data/v3/getByApiCode',
        data: params,
      })
        .then(res => {
          if (res.status === 403) {
            $message({
              type: 'warning',
              message: '当前用户角色缺少【xminer 根据编排API查询数据】权限，请到IAM进行授权',
            });
          }
          if (res && res.data) {
            const _data = res.data.data;
            resolve(_data);
          } else {
            resolve([]);
          }
        })
        .catch(err => {
          resolve([]);
        });
    });
  },

  // 获取当前区域所有上级和下一级区域数据
  getCurrentRegionPrtChdRegs(areaCode) {
    return new Promise(resolve => {
      axios({
        method: 'post',
        url: '/dcp/data/data/v1/web/area/queryGridTree',
        data: [areaCode],
      })
        .then(res => {
          if (res.status === 403) {
            $message({
              type: 'warning',
              message: '当前用户角色缺少【xminer 数据查询】或【xminer 数据管理】权限，请到IAM进行授权',
            });
          }
          if (res && res.data) {
            const { data } = res.data;
            resolve(data);
          } else {
            resolve([]);
          }
        })
        .catch(err => {
          resolve([]);
        });
    });
  },
  queryGridByName(areaName, loginRegionCode) {
    return window._xvueAxios({
      method: 'post',
      url: `/dcp/data/data/v1/web/area/queryGridByName?areaName=${encodeURIComponent(areaName)}`,
      headers: {
        'for-tenant': 1,
      },
      data: [loginRegionCode],
    });
  },
};
